package dfsketches.gr3d;

public class LinearSpline extends Spline {

	public LinearSpline(SplinePoint[] points) {
		super(points);
	}
	
	public LinearSpline(SplinePoint[] points, boolean isLooped) {
		super(points, isLooped);
	}
	
	@Override
	public Matrix3D getTransformation(float time, Matrix3D m) {
		float end = pts[pts.length - 1].time;
		if (!looped) {
			if (time > end) {
				m.copy(pts[pts.length - 1].matrix);
				return m;
			} else if (time < 0) {
				m.copy(pts[0].matrix);
				return m;
			}
		} else if ((time < 0) || (time > end)) {
			time -= Math.floor(time / end) * end;
		}

		int n = binSearch(time);
		Matrix3D m1 = pts[n].matrix;
		Matrix3D m2 = pts[n + 1].matrix;
		float l = pts[n + 1].time - pts[n].time;
		float k_ = (time - pts[n].time) / l;
		float k = (pts[n + 1].time - time) / l;
		m.m11 =	m1.m11*k + m2.m11*k_;
		m.m12 = m1.m12*k + m2.m12*k_;
		m.m13 = m1.m13*k + m2.m13*k_;
		m.m14 = m1.m14*k + m2.m14*k_;
		m.m21 = m1.m21*k + m2.m21*k_;
		m.m22 = m1.m22*k + m2.m22*k_;
		m.m23 = m1.m23*k + m2.m23*k_;
		m.m24 = m1.m24*k + m2.m24*k_;
		m.m31 = m1.m31*k + m2.m31*k_;
		m.m32 = m1.m32*k + m2.m32*k_;
		m.m33 = m1.m33*k + m2.m33*k_;
		m.m34 = m1.m34*k + m2.m34*k_;
		return m;
	}
	
	@Override
	public Matrix3D getTransformation(float time) {
		return getTransformation(time, new Matrix3D());
	}
	
}